﻿Shader "Demo/Unlit/WM_Unlit"
{
    Properties
    {
        _BaseMap ("Texture", 2D) = "white" {}
        _BaseColor ("Colour", Color) = (1, 1, 1, 1)
        // 是否使用 GPU Instancing 来渲染，美术开发时为了可以看到物体该状态必须是关闭的
        [Space(10)] [Toggle(USE_GPU_INST)] use_gpu_inst("Use GPU Instancing", Float) = 0
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline" = "UniversalPipeline"
            "RenderType" = "Opaque"
            "Queue" = "Geometry"
        }

        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "GPUInstancing.hlsl"
        
        #pragma multi_compile __ USE_GPU_INST
        
        CBUFFER_START(UnityPerMaterial)
        float4 _BaseMap_ST;
        float4 _BaseColor;
        CBUFFER_END
        ENDHLSL

        Pass
        {
            Name "Unlit"
            Tags
            {
                "LightMode" = "UniversalForward"
            }

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct a2v
            {
                float4 posOS : POSITION;
                float2 uv : TEXCOORD0;
                float4 color : COLOR;
                uint instanceId : SV_InstanceId;
				// UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 posCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float4 color : COLOR;
				// UNITY_VERTEX_INPUT_INSTANCE_ID
				// UNITY_VERTEX_OUTPUT_STEREO
            };

            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BaseMap);

            v2f vert(a2v input)
            {
                v2f output;
			    // UNITY_SETUP_INSTANCE_ID(input);
                // UNITY_TRANSFER_INSTANCE_ID(input, output);
                // UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

                #ifdef USE_GPU_INST
                float4 posWS = GetInstancingWorldPosition(input.posOS, input.instanceId);
                output.posCS = TransformWorldToHClip(posWS);
                #else
                output.posCS = TransformObjectToHClip(input.posOS);
                #endif

                output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
                output.color = input.color;
                return output;
            }

            float4 frag(v2f input) : SV_Target
            {
                float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv);
                return baseMap * _BaseColor * input.color;
            }
            ENDHLSL
        }
    }
}
